package com.zhiyou100.service.impl;

import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhiyou100.entity.TabDirectory;
import com.zhiyou100.exception.DirectoryException;
import com.zhiyou100.mapper.TabDirectoryMapper;
import com.zhiyou100.service.ITabDirectoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 杨磊
 * @since 2020-10-09
 */
@Service
public class TabDirectoryServiceImpl extends ServiceImpl<TabDirectoryMapper, TabDirectory> implements ITabDirectoryService {

    @Autowired
    private TabDirectoryMapper mapper;

    @Override
    public List<TabDirectory> listByDid(Integer did) {
        if (did == 0) {
            return mapper.selectByMap(
                    Dict.create()
                            .set("dfu", "0")
            );

        }
        // 根据 did 获取其 dfu
        final TabDirectory tabDirectory = mapper.selectById(did);
        // 拼凑的 dfu
        final String dfu = tabDirectory.getDfu() + "-" + tabDirectory.getDid();
        return mapper.selectByMap(
                Dict.create()
                        .set("dfu", dfu)
        );
    }

    @Override
    public TabDirectory selectByDfuAndUidAndDname(TabDirectory directory) {
        final Dict set = Dict.create()
                .set("dfu", directory.getDfu())
                .set("uid", directory.getUid())
                .set("dname", directory.getDname());
        final List<TabDirectory> tabDirectories = mapper.selectByMap(set);
        if (Validator.isNull(tabDirectories) || tabDirectories.size() == 0) {
            return null;
        } else {
            return tabDirectories.get(0);
        }
    }

    @Override
    public void addDirectory(TabDirectory directory) {
        // 获取一个
        TabDirectory tabDirectory = selectByDfuAndUidAndDname(directory);
        // 判断文件夹是否已经存在
        if (Validator.isNotNull(tabDirectory)) {
            throw new DirectoryException("文件夹已经存在！");
        }
        tabDirectory = directory;
        // 设置创建时间
        tabDirectory.setDcreateTime(LocalDateTime.now());
        // 添加文件夹
        mapper.insert(tabDirectory);
    }

    @Override
    public void deleteByDidAndDfuAndDfu(Integer did, String dfu, String dfuLike) {
        // 创建一个查询条件
        // delete from tab_directory where did=#{param1} or dfu=#{param2} or dfu like #{param3}"
        final QueryWrapper<TabDirectory> wrapper = new QueryWrapper<>();
        wrapper.eq("did", did)
                .or()
                .eq("dfu", dfu)
                .or()
                .like("dfu", dfu);
        // 返回影响的行数
        mapper.delete(wrapper);
    }

    @Override
    public void deleteOneByDid(Integer did) {
        String dfu = mapper.selectById(did).getDfu();
        dfu += "-" + did;
        deleteByDidAndDfuAndDfu(did, dfu, dfu + "-%");
    }
}
